<template>
    <div class="login_container">
        <div class="login_box">
            <!--头像区域-->
            <div class="avatar_box">
                <img src="../assets/logo.png">
            </div>
            <!--登录表单区域-->
            <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" label-width="0px" class="login_form">
                <!--用户名-->
                <el-form-item prop="id">
                    <el-input v-model="loginForm.id" prefix-icon="user" placeholder="用户名"></el-input>
                </el-form-item>
                <!--密码-->
                <el-form-item prop="passwd">
                    <el-input v-model="loginForm.passwd" prefix-icon="lock" placeholder="密码" type="password">
                    </el-input>
                </el-form-item>
                <!--按钮区域-->
                <el-form-item class="btns">
                    <el-button type="primary" @click="login">登录</el-button>
                    <el-button type="info" @click="resetLoginForm">重置</el-button>
                </el-form-item>
                <!--链接区域-->
                <el-form-item class="links">
                    <el-link type="primary" @click="register">点击注册</el-link>
                    <el-link type="danger" @click="forget">忘记密码</el-link>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            loginForm: {
                id: '',
                passwd: ''
            },
            //表单的验证规则对象
            loginFormRules: {
                id: [
                    { required: true, message: "请输入用户名", trigger: "blur" }
                ],
                passwd: [
                    { required: true, message: "请输入密码", trigger: "blur" }
                ]
            }
        }
    },
    methods: {
        //重置表单
        resetLoginForm() {
            this.$refs.loginFormRef.resetFields();
        },
        //表单预验证
        login() {
            this.$refs.loginFormRef.validate(async valid => {
                if (!valid) return;
                const { data: res } = await this.$http.post("login", this.loginForm);
                window.sessionStorage.setItem("token", res.token);
                if (res.error_code !== 0) {
                    return this.$message.error(res.message);
                }
                this.$message.success('登录成功');
                if (res.type === 0) {
                    this.$router.push('/home_emp');
                } else if (res.type === 1) {
                    this.$router.push('/home_HR');
                } else if (res.type === 2) {
                    this.$router.push('/home_com');
                } else {
                    this.$router.push('/home_admin');
                }
            });
        },
        //点击跳转到注册页面
        register(){
            this.$router.push('/choice');
        },
        //点击跳转到忘记密码
        forget() {
            this.$router.push('/forget');
        }
    }
};
</script>

<style lang="less" scoped>
.login_container {
    background-color: #2b4b6b;
    height: 100%;
    background-image: url("../assets/1.jpg");
    background-Repeat:'no-repeat';
}

.login_box {
    width: 450px;
    height: 300px;
    background-color: rgba(255, 255, 255, 0.5);
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    .avatar_box {
        height: 130px;
        width: 130px;
        border: 1px solid #eee;
        border-radius: 50%;
        padding: 10px;
        box-shadow: 0 0 10px #ddd;
        position: absolute;
        left: 50%;
        transform: translate(-50%, -50%);
        background-color: #fff;

        img {
            width: 100%;
            height: 100%;
            border-radius: 50%;
            background-color: #eee;
        }
    }
}

.login_form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
}

.btns {
    display: flex;
    justify-content: flex-end;
    float: right;
}

.links {
    float: left;
}

</style>
